Loading texture images from the file system

When you create textures and add texture images to objects in your Kanzi Studio project, Kanzi places these texture images into a kzb binary file. However, you can dynamically load and use in your Kanzi application texture images from the file system of the device where your Kanzi application is running while it is running.

This procedure consists of two parts:

  1. Creating and preparing a Kanzi Studio project. See Creating and preparing a Kanzi Studio project.
  2. Creating the C application in Visual Studio. See Creating the C application in Visual Studio.

Creating and preparing a Kanzi Studio project

To create and prepare a Kanzi Studio project for dynamically loading texture images from the file system:

  1. In Kanzi Studio create a project with C application.
    For example, name the project Loading texture images.
  2. Load a material type that supports textures. See Creating textured materials.
    For example, load TexturedMaterial or PhongTextured material type. Create a material from the material type. You do not have to assign a texture to this material.
  3. Create objects for which you want to dynamically apply textures, add to each object the Texture property, and assign a resource ID to the property.
    For example, create a Box, add to it the Texture property, and assign a resource ID BoxTexture.
  4. Create aliases each pointing to one object you created in the previous step and add all aliases to the resource dictionary. See Using aliases.
    You use aliases in code to get the objects to which you want to assign textures.

  5. In Kanzi Studio select File > Export KZB Binary.
    Kanzi Studio creates the binary and configuration files from your Kanzi Studio project and stores them in <KanziWorkspace>/Projects/<ProjectName>/Application/bin. When you run your Kanzi application from Visual Studio, your Visual Studio solution reads these files to create your Kanzi application.
  6. Place the images you want to use in textures to <KanziWorkspace>/Projects/<ProjectName>/Application/bin.

Creating the C application in Visual Studio

To create the C application for dynamically loading texture images from the file system:

  1. In Visual Studio open the Visual Studio solution for your application located in <KanziWorkspace>/Projects/<ProjectName>/Application/configs/platforms/win32.
  2. Open <ProjectName>.c create the projectLoaded function before kzApplicationConfigure and include the headers for the functions used by the projectLoaded function.
    #include <user/ui/kzu_screen.h>
    #include <user/scene_graph/kzu_object.h>
    #include <user/properties/kzu_fixed_properties.h>
    
    KZ_CALLBACK kzsError projectLoaded(struct KzaApplication* application)
    {
    	kzsError result;
    
    	/* Get the screen node of the application. */
    	struct KzuObjectNode* screenNode = kzuScreenToObjectNode(kzaApplicationGetScreen(application));
    	/* Use the alias to get the object node with an alias. */
    	/* Use the same alias name as you use in your Kanzi Studio project. */
    	struct KzuObjectNode* boxNode = kzuObjectNodeGetRelative(screenNode, "#BoxAlias");
        
    	/* Add the texture resource ID. */
    	/* Use the same file name of the texture image as you use on your file system. */
    	result = kzuObjectNodeAddResource(boxNode, "TextureFromFileSystem", "load_image:///TextureFromFileSystem.png");
    	kzsErrorForward(result);
    
    	/* Set the texture to the boxNode. */
    	result = kzuObjectNodeSetResourceIDProperty(boxNode, KZU_PROPERTY_TYPE_TEXTURE, "TextureFromFileSystem");
    	kzsErrorForward(result);
    
    	kzsSuccess();
    }
  3. Call the projectLoaded function in kzApplicationConfigure when Kanzi loads your application kzb binary files.
    configuration->onProjectLoaded = projectLoaded;
  4. In the <ProjectName>.c create functions imageFileLoader and startup before kzApplicationConfigure and include the headers used by the functions:
    #include <core/memory/kzc_memory_manager.h>
    #include <core/util/image/kzc_image.h>
    #include <user/resource/kzu_image_texture.h>
    #include <user/resource/kzu_texture.h>
    #include <user/ui/kzu_ui_domain.h>
    #include <user/resource/kzu_resource_manager.h>
    
    /* Resource manager protocol handler that is used with "load_image" protocol and which loads an image */
    /* identified by the path from the file system and creates a texture from it. */
    KZ_CALLBACK static kzsError imageFileLoader(struct KzuResourceManager* resourceManager, kzString resourceURL,
    	kzString protocol, kzString hostname, kzUint port, kzString path,
    	void* userData, struct KzuResource** out_resource)
    {
    	kzsError result;
    
    	/* Get the memory manager of the resource manager. */
    	struct KzcMemoryManager* memoryManager = kzcMemoryGetManager(resourceManager);
    	struct KzcImage* image;
    	struct KzuImageTexture* texture;
    
    	/* Load vertically flipped image (for OpenGL). */
    	result = kzcImageLoadResourceFlipped(memoryManager, path, &image);
    	kzsErrorForward(result);
    
    	/* Create a texture from the loaded image. */
    	result = kzuImageTextureCreateFromImage(resourceManager, "Texture from the file system", 
    		image, KZU_TEXTURE_FILTER_BILINEAR, KZU_TEXTURE_WRAP_REPEAT, 0.0f, &texture);
    		kzsErrorForward(result);
    
    	*out_resource = kzuImageTextureToResource(texture);
    	kzsSuccess();
    }
    
    KZ_CALLBACK kzsError startup(struct KzaApplication* application)
    {
    	kzsError result;
    
    	/* Get the resource manager. */
    	struct KzuResourceManager* resourceManager = kzuUIDomainGetResourceManager(kzaApplicationGetUIDomain(application));
    
    	/* Register a protocol handler that the resource manager uses to load */
    	/* images from the file system. */
    	result = kzuResourceManagerRegisterProtocolHandler(resourceManager, "load_image", imageFileLoader, KZ_NULL);
    	kzsErrorForward(result);
    
    	kzsSuccess();
    }
  5. Call the startup function in kzApplicationConfigure when the application is launched.
    configuration->onStartup = startup;
  6. In Visual Studio select one of the available debug solution configurations and run your application.
    For example, select ES2_IMG_Debug solution configuration.

    When you launch the application, Kanzi loads the texture images stored in <KanziWorkspace>/Projects/<ProjectName>/Application/bin and creates textures from the images. When Kanzi loads the application kzb binaries, it sets the textures to the objects you get in the projectLoaded function.

See also

Using kzb binaries